package com.project.thread.demo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Demo1 implements Runnable {

    private int ticket = 20;

    private Lock lock = new ReentrantLock();

    @Override
    public void run() {

        while (true) {

            if (ticket <= 0) {
                return;
            }
            getTicket();

        }

    }

    private void getTicket() {
        lock.lock();
        if (ticket == 0) {
            return;
        }
        System.out.println(Thread.currentThread().getName() + "-->售出第：" + (21 - ticket) + "票");
        ticket--;
        System.out.println("剩余票数：" + ticket + "票");
        lock.unlock();

    }

    public static void main(String[] args) {


        Demo1 demo1 = new Demo1();
        new Thread(demo1).start();
        new Thread(demo1).start();
        new Thread(demo1).start();
//        ExecutorService pool = Executors.newFixedThreadPool(10);
//        pool.execute(new Thread(demo1));
//        pool.shutdown();


    }
}
